package com.adv.crm.controller;

import com.adv.crm.beans.*;
import com.adv.crm.controller.utils.ControllerUtils;
import com.adv.crm.services.AccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * Created by 16978 on 2016/6/18.
 */
@Controller
@RequestMapping("account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("index")
    public String indexView() {
        return "account";
    }

    @RequestMapping("query")
    public String queryView() {
        return "account";
    }

    @RequestMapping("json/index")
    @ResponseBody
    public WarpPageResponse<List<AccountBean>> getIndexData(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "20") int pageSize) {
        WarpPageResponse<List<AccountBean>> response = new WarpPageResponse<List<AccountBean>>();
        List<AccountBean> accountList = accountService.getAccountsByPage(pageSize, pageIndex);
        PageBean page = accountService.getPageInfo(pageSize, pageIndex);
        response.setData(accountList);
        response.setPage(page);
        return response;
    }

    @RequestMapping("json/query")
    @ResponseBody
    public WarpPageResponse<List<AccountBean>> queryData(String accountNameArea, String accountIdArea, AccountQueryBean bean) {
        WarpExtendResponse<List<AccountBean>> response = new WarpExtendResponse<List<AccountBean>>();

        boolean needFilter = false;

        if (StringUtils.isNotBlank(accountIdArea)) {
            bean.setAccountIds(Arrays.asList(accountIdArea.split("\\r\\n")));
            needFilter = true;
        }
        if (StringUtils.isNotBlank(accountNameArea)) {
            bean.setAccountNames(Arrays.asList(accountNameArea.split("\\r\\n")));
            needFilter = true;
        }
        List<AccountBean> accountList = accountService.queryAccountsByPage(bean);
        response.setData(accountList);
        response.setPage(accountService.queryPageInfo(bean));
        if (needFilter) {
            ControllerUtils.filterResponse(response, bean);
        }
        return response;
    }

    @RequestMapping("add")
    public WarpResponse<String> addAccount() {
        WarpResponse<String> resp = new WarpResponse<String>();
        AccountBean accountBean = new AccountBean();
        accountBean.setAccountId("sssWxsdfasdfsa");
        accountBean.setAccountName("test");
        accountService.addAccount(accountBean);
        return resp;
    }

    @RequestMapping("find")
    @ResponseBody
    public WarpResponse<AccountBean> getAccountById(long id) {
        WarpResponse<AccountBean> response = new WarpResponse<AccountBean>();
        if (id < 0) {
            response.setSuccess(false);
            response.setMsg("id不能为null");
            response.setCode(-1);
        } else {
            response = accountService.getAccountById(id);
        }
        return response;
    }

    @RequestMapping("update")
    @ResponseBody
    public WarpResponse<Boolean> updateAccount(AccountBean bean) {
        WarpResponse<Boolean> response = new WarpResponse<Boolean>();
        if (bean == null) {
            response.setSuccess(false);
            response.setMsg("对象不能为null");
            response.setCode(-1);
        } else {
            response = accountService.updateAccountById(bean);
        }
        return response;
    }

    @RequestMapping(value = "index-data")
    @ResponseBody
    public WarpResponse<List<AccountBean>> indexJsonData(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "20") int pageSize) {
        WarpPageResponse<List<AccountBean>> response = new WarpPageResponse<List<AccountBean>>();
        response.setData(accountService.getAccountsByPage(pageSize, pageIndex));
        response.setPage(accountService.getPageInfo(pageSize, pageIndex));
        return response;
    }
}
